home *** CD-ROM | disk | FTP | other *** search
/ Aminet 12 / Aminet 12 (1996)(GTI - Schatztruhe)[!][Jun 1996].iso / Aminet / dev / lang / HeliOS4.lha / helios_demo_disk4 / tutorials / ForthLanguage.doc next >
Encoding:
Text File  |  1995-02-25  |  41.7 KB  |  794 lines

  1.  
  2.      ******************************************************
  3.      FORTH as an example of threaded interpretive languages
  4.      ******************************************************
  5.  
  6.      The HeliOS language is a member of the family of computer languages
  7.      known as "threaded interpretive languages".
  8.  
  9.      The most well known of these languages is FORTH, and HeliOS contains
  10.      many features which it shares with FORTH.
  11.  
  12.      In fact, HeliOS was originally designed as a Super-FORTH system for
  13.      the Amiga, and although it has now developed many unique features,
  14.      most of the old standard FORTH functions are still implemented.
  15.  
  16.      All HeliOS users will benefit from a knowledge of FORTH, so here is
  17.      a brief history and description of this interesting language.
  18.  
  19.      *******************
  20.      FORTH and the Amiga
  21.      *******************
  22.  
  23.      Before going on to a general discussion on "what FORTH is", it is
  24.      worth mentioning in advance those special qualities which make it
  25.      such an an ideal programming language for use on the Amiga.
  26.  
  27.      The Amiga is probably the ultimate "value for money" computer so far
  28.      produced for domestic use, and irrespective of price many of us feel
  29.      it has a special character which sets it apart from all its rivals.
  30.      It is above all an enthusiast's machine, with many unique hardware
  31.      capabilities, effective multi-tasking, and a lot of very thoughtfully
  32.      evolved and conceptually advanced support system software.
  33.  
  34.      With such a multi-faceted machine as the Amiga there is always the
  35.      feeling that there is much untapped potential still to be explored:
  36.      programmers on the Amiga have a huge range of hardware and software
  37.      functions at their disposal, but not all of them are easy to access.
  38.  
  39.      In order to use all these fascinating special features of the Amiga
  40.      there is inevitably a fair amount of learning to do.  Worse still,
  41.      there are very few user friendly programming languages available on
  42.      the Amiga which allow the ordinary non-expert computer enthusiast to
  43.      experiment and learn the machine in an interactive way whilst also
  44.      supplying the necessary speed and power.  Notice the use of the term
  45.      "interactive", which means that you can carry out programmed commands
  46.      with instant response in real time: this is much more useful than the
  47.      tedious compilation process of a language such as "C".
  48.  
  49.      The really major assets of FORTH are that it allows easy real-time
  50.      experimentation at the keyboard, instant compilation, and tremendously
  51.      fast compiled code speed.  FORTH based languages are all wonderfully
  52.      interactive in use, which makes them ideal for creative programming
  53.      and certainly worth exploring as potentially ideal Amiga languages.
  54.  
  55.      You may still wonder why you should bother to learn a new and rather
  56.      unusual language like FORTH when there are already good versions of
  57.      "C" and BASIC on the Amiga, as well as excellent assembler systems?
  58.  
  59.      Well....BASIC is never going to allow you to exploit the Amiga to its
  60.      full capability, for reasons of speed, efficiency, and the restrictive
  61.      design of the language.  The other main contender, "C", is not at all
  62.      user-friendly, it is not at all interactive, it is not fast in terms
  63.      of program development or code execution speed, and it is not an easy
  64.      language to learn or use.  Assembly language is good for speed and is
  65.      ideally suited to small-scale specialist code fragments: however, in
  66.      all other respects it is hardly a candidate for serious development
  67.      as it is so clumsy and difficult to use for larger scale projects.
  68.  
  69.      What is needed for creative Amiga programming is an easy-to-use, fully
  70.      interactive and very fast language with efficient support for all
  71.      aspects of Amiga software functionality.  FORTH is potentially just
  72.      such a language, and HeliOS is a specialised version of this language
  73.      dedicated to providing a perfect Amiga-specialised FORTH environment.
  74.      HeliOS is very fast and efficient, and it is fully Amiga compatible
  75.      because it has been totally dedicated to and developed for the Amiga.
  76.  
  77.      If you are tempted to believe the usual claims that "C" is the only
  78.      "serious" Amiga language, and assembler is necessary for fast game
  79.      programming, then you might be intrigued to see just what HeliOS is
  80.      capable of.  It is a fact that using HeliOS you can do things which
  81.      would be quite impossible with any other language, and furthermore
  82.      you can work in the most user-friendly and productive environment of
  83.      any Amiga programming system.
  84.  
  85.  
  86.      *************************
  87.      A little history of FORTH
  88.      *************************
  89.  
  90.      In the late 1960s Charles Moore, a truly original and innovative
  91.      thinker, introduced a computer language which embodied a radically
  92.      new approach to software design.
  93.  
  94.      He called his new language FORTH, for a slightly picturesque reason:
  95.  
  96.      "I was working on an IBM 1130, a "third generation" computer. The
  97.      result seemed so powerful that I considered it a "fourth generation
  98.      computer language". I would have called it FOURTH, except that the
  99.      1130 permitted only five character identifiers. So FOURTH became
  100.      FORTH, a nicer play on words anyway."
  101.  
  102.      This little "play on words" gives an interesting insight into the
  103.      character of Charles Moore and his programming language: FORTH is very
  104.      clever and sophisticated in the way it uses natural human language to
  105.      evolve human-to-computer communication via natural human linguistic
  106.      constructs.  FORTH is a very "linguistically oriented" language, and,
  107.      although you might think this is true of all languages, an examination
  108.      of any "C", BASIC, or assembler source code will reveal that these
  109.      languages are cryptic, formalised, and very "un-human" in expression.
  110.  
  111.      Computer languages necessarily started out as simple and cryptic
  112.      systems for manipulating available computing hardware, and initially
  113.      computers were necessarily very crude.  As knowledge and hardware
  114.      evolved there gradually arose the potential for development of more
  115.      sophisticated computer languages, but unfortunately the computer
  116.      industry is generally conservatively inclined towards building only
  117.      on existing concepts, mostly for commercial reasons.
  118.  
  119.      Moore was unhappy with the software tools available to him at that
  120.      time, which in many cases were the already outmoded products of early
  121.      computer development work.  The software industry seemed ripe for a
  122.      new outlook based on the experience gained in early years of explosive
  123.      commercial progress.
  124.  
  125.      Like all idealists Moore may have thought that the world of computing
  126.      would welcome a new and better approach, or perhaps he took pleasure
  127.      in pure creativity independent of any reliance on traditional methods.
  128.      Certainly he made no attempt to embody traditional practices in his
  129.      new language and undoubtedly this originality was a liability in what
  130.      is still a very conservative industry.
  131.  
  132.      Moore set out to achieve two broadly compatible aims.
  133.  
  134.      Firstly, he wanted to make communication with a computer more natural
  135.      and intuitively satisfying: he wanted to write better software faster
  136.      and more enjoyably.
  137.  
  138.      Secondly, he tried to satisfy the technical criteria of elegance,
  139.      speed, compactness, efficiency, and portability.
  140.  
  141.      He succeeded admirably, but his language unfortunately never achieved
  142.      widespread popularity in a commercial environment geared to plodding
  143.      conservatism rather than innovation and individual perfectionism.
  144.  
  145.      Moore was a rare blend of enthusiast, perfectionist, and philosopher,
  146.      and sadly enough his FORTH language has largely remained the province
  147.      of precisely that kind of computer enthusiast.
  148.  
  149.      The computing industry is organised in a way which tends, inevitably,
  150.      to resist change and unfortunately FORTH never made the breakthrough
  151.      into popular general usage.  Amazingly enough, the computer languages
  152.      in general use today suffer from the same ridiculous limitations as
  153.      were apparent to Charles Moore in the late 1960s; indeed the very same
  154.      languages are still in use fundamentally unchanged.  FORTH is still as
  155.      unique today as it was in 1970, still an "odd man out", and still well
  156.      worth investigating if you are prepared to try something different.
  157.  
  158.      The fact that there are still FORTH systems in use today is probably
  159.      due to the general credibility which FORTH gained when it became the
  160.      adopted language of the scientific Astronomical community.  FORTH has
  161.      been widely used in observatories for critical software systems used
  162.      in the precise tracking control of telescopes, and is also used for
  163.      demanding instrumentation control applications.
  164.  
  165.      As far as home computers are concerned FORTH is still a contender on
  166.      the fringe of the software industry, especially among high performance
  167.      software enthusiasts, and manages to keep several currently available
  168.      implementations on most microcomputers.
  169.  
  170.      Unfortunately, as explained later, some of these "domestic" versions
  171.      of FORTH have hardly done justice to the true power of the language.
  172.      Nevertheless, FORTH is such an unusual but ultimately natural way of
  173.      communicating with a computer that those who come into contact with it
  174.      tend to become extremely enthusiastic about it.
  175.  
  176.      In Moore's own words again:
  177.  
  178.      "FORTH provides a natural means of communication between man and the
  179.      smart machines he is surrounding himself with.
  180.  
  181.      This requires that it share characteristics of human languages,
  182.      including compactness, versatility, and extensibility.
  183.  
  184.      I cannot imagine a better language for writing programs, expressing
  185.      algorithms, or understanding computers."
  186.  
  187.  
  188.      **********************************
  189.      Why is FORTH different and better?
  190.      **********************************
  191.  
  192.      One of the prime features designed into FORTH by Moore was simplicity
  193.      and an excellent immediate interpretive mode of operation.  He always
  194.      sought simple and elegant solutions and had a strong intrinsic belief
  195.      in the value of being able to interact directly with the computer at
  196.      the keyboard.  It is useful to have a fast, efficient, interactive and
  197.      humanly readable language to help us tame the problems associated with
  198.      using a complex machine to the limits of its capabilities.
  199.  
  200.      FORTH allows easy one-to-one communication with the computer in real
  201.      time - it is wonderfully "interactive" and this means that it can
  202.      also be FUN to use!  You can PLAY with the computer at the keyboard
  203.      using FORTH with a freedom which you could never achieve using any
  204.      other programming language.
  205.  
  206.      When faced with the Amiga, which is not exactly a simple machine, one
  207.      sometimes feels in real need of a tool to simplify and facilitate
  208.      interactive communication, instead of relying on the tedious process
  209.      of using a clumsy compiled language such as "C".
  210.  
  211.      Certainly there would be many more creative and original software
  212.      innovations if it were made easier to experiment at the keyboard.
  213.      Obviously a machine like the Amiga has such advanced possibilities
  214.      that it is necessary to use the very best methods available in order
  215.      to exploit its special hardware and software capabilities to the full.
  216.      In common with many complex human activities it is useful to be able
  217.      to experiment and "play" interactively while exploring the various
  218.      possibilities and learning to use the full potential of the computer.
  219.  
  220.      Interactivity is great, but of course it is no use having easy-to-use
  221.      interactive tools if they are ultimately not also powerful enough to
  222.      exploit all aspects of the computer.  Many easy-to-use BASIC languages
  223.      allow you to do some things quite easily, but at the expense of speed
  224.      and without the full freedom to explore every aspect of the Amiga's
  225.      hardware capability.
  226.  
  227.      FORTH is much more powerful than BASIC, it is fully interactive as
  228.      well as being a compiler, it is capable of direct hardware control
  229.      as well as high level operating system programming, and it has all
  230.      the capabilities of both "C" and "Assembler" built into one system.
  231.  
  232.      Furthermore, FORTH is the most suitable computer language available
  233.      for learning and experimenting because it uses a very intuitive and
  234.      flexible linguistic approach to computer communication: using FORTH
  235.      you teach your computer to speak YOUR language and build your own
  236.      personalised version of the language.
  237.  
  238.      ***************
  239.      Reputations....
  240.      ***************
  241.  
  242.      In spite of its reputation for compactness and speed, and in spite of
  243.      its cult status with those who have grown to appreciate its subtlety,
  244.      FORTH has retained an ill deserved reputation for incomprehensibility.
  245.      You may have read journalistic accounts of FORTH's peculiar and maybe
  246.      "difficult" style of source code but don't be put off by these ill
  247.      founded rumours - they are just simply not true!
  248.  
  249.      The fact that FORTH has been criticised over "legibility" is an amazing
  250.      state of affairs when you consider that this kind of accusation is
  251.      rarely, if ever, levelled at "C", which really is an extremely poorly
  252.      designed language in terms of source code readability.
  253.  
  254.      The problem is that FORTH is "different" (it is unique, in fact), and
  255.      those familiar with what have become "conventional" concepts sometimes
  256.      seem to find it hard to change and appreciate what are actually greatly
  257.      superior characteristics.  It is easier to dismiss something unusual
  258.      as a "curiosity" than it is to take the trouble to understand it.
  259.  
  260.      FORTH has very often suffered from misleading and offputting publicity
  261.      concerning its use of reverse polish notation, but in reality a little
  262.      understanding of the underlying principles of FORTH programming soon
  263.      makes the whole system seem most natural and even obvious.
  264.  
  265.      Try FORTH without prejudice, and try to forget most of what you may
  266.      already know about BASIC and "C": FORTH is a whole new world!
  267.  
  268.  
  269.      **********************************
  270.      Past unfortunate incarnations.....
  271.      **********************************
  272.  
  273.      Perhaps you may have come across FORTH in one of its early incarnations
  274.      on the popular 8-bit micros, and it must be admitted that many of these
  275.      primitive implementations did it little justice and were often virtually
  276.      unusable.
  277.  
  278.      Those brave enough to try FORTH on these systems have suffered at the
  279.      hands of some terrible implementations which turned FORTH into a very
  280.      frustrating experience.  These inadequte FORTH implementations made the
  281.      language practically unusable except as a mildly interesting curiosity.
  282.  
  283.      Many of the commercially available 8-bit FORTHS were quite unworthy of
  284.      the name, so if you have had experience with these inferior versions
  285.      then you should at least be prepared to give FORTH another chance.
  286.  
  287.  
  288.      **************
  289.      FORTH concepts
  290.      **************
  291.  
  292.      FORTH is linguistically sophisticated and flexible
  293.  
  294.      FORTH is user-extensible
  295.  
  296.      FORTH is super-fast
  297.  
  298.      FORTH is very memory efficient
  299.  
  300.      FORTH is an interpreter and a compiler
  301.  
  302.      FORTH incorporates fast stack-based parameter passing
  303.  
  304.      FORTH uses efficient reverse-polish notation
  305.  
  306.      FORTH is easily customisable to individual taste
  307.  
  308.      FORTH is interactive (and therefore fun and creative to use)
  309.  
  310.      FORTH embodies many unique innovative programming tools and concepts
  311.  
  312.      In operation FORTH threads together fast specialised machine code
  313.      routines to produce a high level interactive language with a minimum
  314.      of "overhead" by allowing you to associate human language words with
  315.      computer routines in a simple and direct way.
  316.  
  317.      FORTH code is not only very nearly as fast as "pure" machine code, but
  318.      incredibly enough it is actually also more compact in many cases than
  319.      a corresponding assembly language program.
  320.  
  321.      One unique feature of FORTH is that it allows the language itself to
  322.      be customised to your own requirements: using FORTH as a basis you can
  323.      literally build your own required new features into a really usable and
  324.      powerful "personalised" programming toolkit.  Every function in FORTH
  325.      can be renamed however you wish, and the real power of the language
  326.      lies in its extreme configurabilty.
  327.  
  328.      FORTH is subtle but not over-complicated in the inner details of its
  329.      implementation and this means that it is possible to learn easily how
  330.      to ideally maximise the efficiency of your code.  To use FORTH really
  331.      productively it is well worth learning how the language works at the
  332.      innermost level, and this will be well within the capabilities of most
  333.      enthusiasts.
  334.  
  335.      FORTH allows the user get to grips with its inner workings in a way
  336.      which is impossible with other languages, and this gives a freedom to
  337.      FORTH programmers to use very powerful and ingenious methods usually
  338.      not available in languages other than assembler: knowledge is power!
  339.  
  340.  
  341.      ******************************
  342.      FORTH's unique use of language
  343.      ******************************
  344.  
  345.      The basic component of FORTH programming is the "word", and FORTH
  346.      programs are structured very much according to the natural logical
  347.      flow of human liguistic expression.
  348.  
  349.      A FORTH word is a coded function with a name which, when passed to
  350.      the interpreter, will generate a particular software command.
  351.  
  352.      Each word can either be associated with a pure machine language routine
  353.      or can be built up from a sequence of other previously defined FORTH
  354.      words.
  355.  
  356.      FORTH programming consists largely of the process of defining, refining,
  357.      and combining words into ever more complex and powerful expressions.
  358.  
  359.      FORTH is by its very nature extensible and malleable, and it has been
  360.      justly said that programming in FORTH consists in remodelling the very
  361.      language itself.  Writing FORTH programs is a process of building a
  362.      communication channel between you and the computer, and in the process
  363.      you actually "teach" the machine to respond to "words" of your choice.
  364.  
  365.      One interesting result of the way FORTH works is that the language
  366.      can itself be built up in many different ways: all FORTH systems are
  367.      NOT equal, and all individual FORTH programmers tend to evolve very
  368.      personal styles.  FORTH systems can be designed in specialised forms
  369.      to perform optimally depending on the needs of differing applications.
  370.  
  371.      In designing an ideal FORTH system modelled especially for use on the
  372.      Amiga, it is necessary to concentrate on those factors which promote
  373.      superior performance in terms of speed, compactness, versatility, and
  374.      creative useability.  Above all it must be possible to fully exploit
  375.      all the features of the Amiga without any restrictions imposed by the
  376.      language itself, so it is interesting to examine the essential features
  377.      which distinguish FORTH systems of differing capabilities.
  378.  
  379.      The basic characteristics of any FORTH implementation are revealed in
  380.      the choice of command words which are provided as core functions.  The
  381.      vocabulary of the language can differ widely between versions, and
  382.      linguistic considerations can deeply affect system performance.
  383.  
  384.      Like all computer languages FORTH comprises a system for translation
  385.      of human language - words - into the numerical symbolic language which
  386.      the CPU of the computer employs.
  387.  
  388.      FORTH optimises the human language side of the equation more than any
  389.      other computer language and all aspects of FORTH programming revolve
  390.      around the definition and use of words - human language words which
  391.      each user can choose and modify as desired.
  392.  
  393.      You can literally make FORTH source code read like English sentences,
  394.      choosing your own words for every software function and your own style
  395.      of parameter passing.
  396.  
  397.      A good FORTH programmer hardly needs many source code comments because
  398.      the language itself can be virtually as clear and explicit as written
  399.      English - at least to his/her self! (well - theoretically anyway!)
  400.  
  401.      As explained later this ideal state of lucidity is not always easy
  402.      to achieve using small "minimal" FORTH systems.  It is evident that
  403.      the larger your available vocabulary the better you will be able to
  404.      express yourself, and FORTH implementations vary dramatically in the
  405.      size of their vocabularies. An internationally agreed FORTH "standard"
  406.      requires that certain minimal sets of words be present in all FORTH
  407.      systems, but this standard vocabulary is hardly adequate for serious
  408.      work on a machine like the Amiga.  It is important to use a FORTH
  409.      system with an enhanced FORTH vocabulary which allows the writing of
  410.      code which is not only very readable but also more expressive and
  411.      efficient in terms of both compactness and speed.
  412.  
  413.      The size and quality of the set of pre-defined words which you get as
  414.      the starting command set in any FORTH system determines the ability of
  415.      that system to generate efficient code.  Having an extensive and well
  416.      designed set of pre-programmed commands is vitally important, and on
  417.      the Amiga it is best if a FORTH implementation includes commands for
  418.      accessing all the special graphical and sound functions provided by
  419.      the computer hardware and operating system.
  420.  
  421.      If a particular FORTH implementation has a poorly designed inbuilt set
  422.      of commands it will often be necessary to use compound constructions
  423.      to achieve a desired result.  In these cases much of the programming
  424.      overhead may contribute little to the logical task in hand, and since
  425.      it takes time for the computer to carry out each command it is always
  426.      more efficient to avoid strings of words where one word will suffice.
  427.  
  428.      Think of your FORTH system as an "alien being" with whom you need to
  429.      learn to communicate, and think of the basic inbuilt command set as
  430.      the vocabulary which you both already understand.  If the version of
  431.      FORTH you are using has an excellent vocabulary already you will need
  432.      to spend less time struggling to express yourself.
  433.  
  434.      It is interesting to note that the natural sequence of human thought,
  435.      and its expression in language, tends to come up with very well
  436.      structured formalisations if left to express itself freely.  Everyone
  437.      thinks differently, but everyone thinks efficiently in their own way.
  438.      It is important to have freedom to express ideas in a personal manner
  439.      rather than being restricted by someone else's preconceived system.
  440.  
  441.      In an ideal computer language there would be a true reflection of this
  442.      natural information flow with as few as possible grammatical or formal
  443.      impositions which do not contribute to the main logical sequence.  If
  444.      source code appears incomprehensible and convoluted, particularly to
  445.      an unfamiliar reader, this can be a fair indication that a program will
  446.      prove both slow and inefficient when executed.  Freedom of expression
  447.      is best when a minimum of extraneous functions need to be used simply
  448.      to comply with the needs or conventions of the programming language
  449.      environment rather than the logic of the problem being solved.
  450.  
  451.      A very real advantage of FORTH's free use of language is that it is
  452.      much easier to learn and develop a software system which is expressed
  453.      entirely in your own words, using expressions which have memorability
  454.      and meaning for you, than it is to try to learn someone else's
  455.      formulations.  It is a wonderful asset to any programmer to be able
  456.      express ideas in your own way, with a comfortable and comprehensible
  457.      logical flow of ideas.
  458.  
  459.      It is even better when you can get instant feedback from the computer
  460.      so that you can see at once whether or not your ideas are working out.
  461.      This is especially true of machines like the Amiga where sheer size
  462.      and complexity can make learning new software systems quite arduous,
  463.      and even more so if you have to wait for a long process of compilation,
  464.      linking, and loading before finding that you have made some obscure
  465.      syntactical error.
  466.  
  467.      So, to summarise the unique linguistic nature of FORTH, it could be
  468.      said that FORTH is ideal because not only can you express yourself in
  469.      your own language very freely, but you can also get an instant response
  470.      from the computer on a word-by-word basis, as in a conversation.
  471.  
  472.      *********************
  473.      Human versus computer
  474.      *********************
  475.  
  476.      Above it was stated that FORTH makes uniquely excellent use of human
  477.      language and allows the programmer great freedom of expression and
  478.      interactive experimentation.
  479.  
  480.      Of course, one must not forget that the computer has a "point of view"
  481.      which also needs to be kept in consideration, and the other side of the
  482.      human/machine relation is the unwanted imposition by the high level
  483.      computer language of artificial logical processes on the way the CPU
  484.      of the computer carries out its work.
  485.  
  486.      There is usually a natural and efficient way for the processor to carry
  487.      out any particular software task, and to the extent that a high level
  488.      language forces the CPU to deviate from optimum machine code processes
  489.      the language becomes slow and inefficient.
  490.  
  491.      Not many high level languages come anywhere near optimising their final
  492.      machine code constructs, mainly because they lean quite heavily towards
  493.      cushioning the end user from "bothering" with CPU operations.
  494.  
  495.      FORTH was written for people who understand how computers work and who
  496.      do concern themselves with the nature of CPU operations!
  497.  
  498.      As already explained, FORTH was conceived initially as a super software
  499.      tool designed by a computer professional for his own use, and it does
  500.      assume a little more knowledge of what is going on behind the scenes
  501.      than other languages.  FORTH does require more from the programmer but
  502.      gives a lot in return because you have more control over all processes.
  503.  
  504.      FORTH has been likened to a high level assembler, and from the point
  505.      of view of the CPU FORTH code is very efficient.  It is actually quite
  506.      simple to write pure machine code directly from the high level HeliOS
  507.      interpreter/compiler, and converting individual time-critical routines
  508.      into fast machine code is very easy.
  509.  
  510.  
  511.      ********************
  512.      Having it both ways?
  513.      ********************
  514.  
  515.      All the above seems to be saying, apparently paradoxically, that FORTH
  516.      not only optimises the human side of the equation but also the machine
  517.      side.  Can this really be so?  Amazingly enough it can, but of course
  518.      there are, inevitably in this imperfect world, one or two compromises.
  519.  
  520.      You gain power over the CPU only under threat of severe punishment if
  521.      you make a mistake.  You need to be very careful what you are doing,
  522.      and you will end up understanding intimately how the computer works
  523.      whether you like it or not!  FORTH gives you extreme power but very
  524.      little cushion against any errors you may make: you are controlling
  525.      the computer at a very low level and with very high efficiency.
  526.  
  527.      At least, though, you do get to know your mistakes at once rather than
  528.      waiting half an hour or so to have them compiled and linked before all
  529.      is revealed!  You may not be protected from the inevitable consequences
  530.      of faulty code, but you can at least be reassured that the CPU is not
  531.      wasting valuable time checking for all your possible errors.  You will
  532.      also only write optimised code if you learn to think in a way consistent
  533.      with the logic of the computer, and FORTH is very much a transparent
  534.      link between man and machine - a double edged sword.
  535.  
  536.      *******************
  537.      FORTH functionality
  538.      *******************
  539.  
  540.      Now let us have a very brief look at how the language functions.
  541.  
  542.      The first thing you must realise is that a FORTH system can be built
  543.      and configured in many different ways and in general the main three
  544.      performance criteria of program size, speed, and code portability tend
  545.      to be somewhat mutually exclusive.
  546.  
  547.      All software involves a trade-off between various conflicting factors,
  548.      and different strategies are required when designing a FORTH system
  549.      for modern 16-bit computers as against the tiny machines on which many
  550.      early FORTH systems were developed.
  551.  
  552.      FORTH belongs to the general category of what are called "threaded
  553.      interpretive languages", otherwise abbreviated to "TIL", and these
  554.      essentially use a system of pointers and stacks to control the complex
  555.      threading together of code "units" (words) which themselves consist of
  556.      further threaded sub-sections.
  557.  
  558.      The lowest level FORTH code units are machine coded functions, and are
  559.      very fast: the highest level FORTH commands can be composed of many
  560.      threaded sub-levels, and can be very slow and inefficient.  The way
  561.      a FORTH system is designed and built has huge effects on performance,
  562.      because the core command set of any FORTH can consist of fast low
  563.      level functions or slow high-level "compound" functions.
  564.  
  565.      The machine code functions within a FORTH system are the "Workers",
  566.      and they are controlled by the higher level functions.  A cunningly
  567.      devised FORTH system can contain very few of these "workers", but can
  568.      put them to a very varied and ingenious use.  The problem is that if
  569.      you only have a few "workers" running around doing all the work, the
  570.      system can get rather slow.
  571.  
  572.      It is possible to have a special "Worker" for every function, so that
  573.      every job gets done very quickly and efficiently: this type of system
  574.      consumes more memory and is harder to port from machine to machine,
  575.      but it is lightning fast!  HeliOS is a FORTH based system with more
  576.      pure machine code "Workers" than any other FORTH on any computer.
  577.  
  578.      Higher level FORTH words usually consist of instructions to tell other
  579.      instructions to tell "worker" routines to do something.  This means
  580.      that in FORTH you get a hierarchical "nesting" of levels of command
  581.      and execution, with machine coded "worker" routines combined with
  582.      high level "instructional" routines.  As you can imagine, this can
  583.      turn out to be a somewhat over-bureaucratic system if care is not
  584.      taken to plan for an efficient overall organisation.
  585.  
  586.      There is no limit on "nesting" or combination of levels, high or low
  587.      level constructs being easily and freely combined, and it is not even
  588.      necessarily obvious to what extent any FORTH function combines high
  589.      and low level code.  This leaves a great freedom for FORTH systems to
  590.      vary in character, and it is true to say that designing a good FORTH
  591.      is something of an art form: all FORTHs are different and individual.
  592.  
  593.      To a great extent your programming efforts will build upon the basis
  594.      provided by a set of central core routines in your FORTH system which,
  595.      as explained, may be written in high or low level FORTH code.
  596.  
  597.      For speed it is vital that these central core routines be  written in
  598.      fast machine code rather than high level FORTH.
  599.  
  600.      This distinction must be fully appreciated, so always bear in mind
  601.      that any complex FORTH word can be written at various levels, from
  602.      pure machine code to densely nested high level structures built upon
  603.      very few machine code building blocks.
  604.  
  605.      The FORTH interpreter glides gracefully between the pure machine code
  606.      and a form of vectored execution of listed code subsections.
  607.  
  608.      These code subsections can themselves be more lists of further nested
  609.      subsections, and the inner interpreter will thread everything together
  610.      effortlessly without you having any indication whether it is whizzing
  611.      through pure fast machine code or churning its way up and down many
  612.      levels of convoluted vector list nestings.
  613.  
  614.      All this means that you need to know what you are doing with FORTH:
  615.      the language will allow you to write very good or very bad code!
  616.  
  617.      Interestingly, self-reference, recursion, and all the subtle concepts
  618.      currently popular in Artificial Intelligence can be implemented very
  619.      easily and well in FORTH, possibly rather more efficiently, in fact,
  620.      than in specialised "AI" languages such as "LISP".
  621.  
  622.      ************************
  623.      Implementations of FORTH
  624.      ************************
  625.  
  626.      One result of the way threaded interpreters work is that FORTH can run
  627.      with surprisingly few distinct machine code routines, which makes it
  628.      very easy to produce in readily machine-portable form.
  629.  
  630.      FORTH can be built like an inverted pyramid on a foundation of a very
  631.      small number of machine code routines, but in these circumstances the
  632.      language will run relatively slowly.  Faster FORTH systems will use
  633.      more pure machine coded core functions, and also more memory.  It is
  634.      entirely the responsibility of individual FORTH system designers to
  635.      decide whether core functions should be implemented in fast machine
  636.      code or compact high level FORTH code.
  637.  
  638.      This means that there can be a huge difference in speed performance
  639.      between two FORTH systems which, in terms of the functions provided,
  640.      appear to be identical.
  641.  
  642.      Remember that FORTH high level code is very compact, so although it
  643.      runs more slowly than a heavily machine coded FORTH system, a high
  644.      level FORTH encoded system will be incredibly memory efficient.
  645.  
  646.      This feature can be put to good service if used judiciously and can
  647.      enable you to combine extremely fast code in time-dependent routines
  648.      with extremely compact code in memory critical areas.
  649.  
  650.      It is worth mentioning here that most implementations of FORTH are
  651.      based on an early version of the language intended for use on small
  652.      computers, with easy portability from one machine to another and very
  653.      thrifty memory usage the major design criteria.
  654.  
  655.      With these aims in view, this type of implementation carries a very
  656.      bare minimum of machine-specific machine language code, building the
  657.      majority of its functions by using FORTH code to build on other FORTH
  658.      code. This is a very memory efficient system and is amazingly compact
  659.      and easy to port between machines. It is actually a triumph of design
  660.      for the specific purpose for which it was written and can be used in
  661.      incredibly tiny memory spaces as well as being easily adapted to any
  662.      type of programming application.
  663.  
  664.      Memory economy and portability may seem quite a good idea, but on an
  665.      Amiga speed is a more important criterion, there is plenty of available
  666.      memory space, and portability is not generally an issue.
  667.  
  668.      Since all FORTH functions can be coded in high level compiled FORTH
  669.      code or pure machine code, the extent to which the designer of a FORTH
  670.      system uses fast machine code in the main core routines determines the
  671.      speed and efficiency of programs written using that implementation.
  672.  
  673.      A good Amiga FORTH certainly requires a tightly written fast machine
  674.      code implementation which doesn't worry too much about using up a few
  675.      extra bytes in the quest for speed.
  676.  
  677.      Source code editing format has often also been a point of contention
  678.      between FORTH enthusiasts of differing persuasions, and once again an
  679.      Amiga implementation of the language has different requirements from
  680.      an early 8-bit micro version.
  681.  
  682.      It was for reasons of memory economy on tiny machines that originally
  683.      a "virtual memory" disk-based source code editing and storage system
  684.      was used in many early FORTHs.  This system imposed very severe source
  685.      code format restrictions which many FORTH enthusiasts became accustomed
  686.      to, making them unwilling to move on to more freely expressive systems.
  687.  
  688.      The old ingenious but often crude system traditionally incorporated
  689.      only a primitive source code editing facility, and this has been an
  690.      anachronism which has crippled many versions of FORTH, especially on
  691.      small 8-bit computers.
  692.  
  693.      On the Amiga this kind of "primitive" system is totally unacceptable
  694.      and must be avoided at all costs.  It is absolutely essential to have
  695.      a good, fast, full-featured source code editor which is totally memory
  696.      resident if you are going to manage any kind of really efficient code
  697.      development.
  698.  
  699.      The use of source code "screens" in FORTH, which you may see mentioned
  700.      in books, is another throwback to the old disk buffering systems, and
  701.      should be avoided since it places unnecessary constraints on the format
  702.      of your source code.
  703.  
  704.      It is best to employ a system which accepts source code text in free
  705.      format so that you can write your programs in a style which you find
  706.      comfortable and readable to suit your own taste.
  707.  
  708.      *********************************
  709.      FORTH program structure and style
  710.      *********************************
  711.  
  712.      In its purest form the code of a compiled FORTH program consists of a
  713.      series of tightly written machine code routines, provided in the FORTH
  714.      core, threaded together using very short connecting routines which
  715.      hardly compromises performance at all.
  716.  
  717.      However, every division of a program into a sub-word does carry a small
  718.      but real connective overhead, so the optimum strategy is to avoid
  719.      lots of short routines patched together every time a commonly needed
  720.      task is performed.  Ideally, a pre-defined pure machine coded core
  721.      routine for every important and commonly used function helps cut down
  722.      language overheads to a minimum.  Obviously this takes extra memory
  723.      space, but this is a commodity which can, within reason, be afforded
  724.      as a trade off in the more important quest for speed.
  725.  
  726.      In particular, internal housekeeping tasks such as stack handling need
  727.      to be kept as short as possible, and the "traditional" FORTH method of
  728.      using combinations of a very few primitive functions is not ideal.
  729.  
  730.      While traditional FORTH's very few stack control words may be patched
  731.      together to create very powerful compound words, this can cause a very
  732.      significant and unnecessary loss of speed. The Amiga has enough memory
  733.      to have a comprehensive fast-coded set of system routines pre-prepared,
  734.      and again these should be part of an efficiently machine-coded FORTH
  735.      core vocabulary.  It is in areas such as this that HeliOS departs from
  736.      standard FORTH by providing a very extended core command set.
  737.  
  738.      Incidentally it is perhaps worth pointing out here that the excessive
  739.      combination of primitive FORTH stack control words produces code which
  740.      is not only slow, but also unreadable, inefficient and hard to debug.
  741.      You will see examples of this inefficient technique in many eary FORTH
  742.      text books, and it has often seemed to be a masochistic obsession of
  743.      FORTH programmers to write long convoluted sections of code which do
  744.      nothing other than rearrange the system stack using totally confusing
  745.      and unreadable combinations of a few stack control primitives!
  746.  
  747.      Reading some FORTH texts it sometimes seems as though stack management
  748.      by convoluted means is the essence of clever FORTH programming, whereas
  749.      in truth such complex stack manipulation is literally a waste of CPU
  750.      time and should be kept to a bare minimum.
  751.  
  752.      Don't be tempted into seemingly "clever" juggling with multiple items
  753.      on the stack; keep stack usage simple, fast, and elegant, and you will
  754.      find that a prime source of confusion and error will be eradicated.
  755.  
  756.      Early FORTH systems which only provided a very austere and crude set
  757.      of core functions required a lot of extension work to create all the
  758.      tools required for serious programming.  Even if you were prepared and
  759.      able to write these extensions yourself, or even if the extensions were
  760.      made available in the form of FORTH source libraries, the bad news was
  761.      that these were compiled as high level constructs and carried a very
  762.      significant speed penalty over low level core machine code routines.
  763.  
  764.      HeliOS provides a unique and very extensive set of specialised words,
  765.      but these will only help you if you take the time and trouble to learn
  766.      them.  You can of course ignore all these specialised words and merely
  767.      use the simple set of basic traditional FORTH functions, but doing so
  768.      will result in a severe performance penalty.  This penalty will then
  769.      overspill into all your subsequent code, so always remember that high
  770.      performance really starts with a thorough mastery of the wide range of
  771.      detailed HeliOS commands.
  772.  
  773.      Remember that high level FORTH code is by nature very compact indeed
  774.      and even an unexpanded Amiga A500 is a HUGE computer for a FORTH
  775.      environment: so forget traditional FORTH purist ideas concerning the
  776.      "elegance" of managing complex tasks using a tiny array of words. It
  777.      is a good idea to build yourself a really comprehensive set of fast
  778.      software tools and indulge in the luxury of a language to a large
  779.      extent designed by you with your own specific needs in mind.
  780.  
  781.      ***************
  782.      Further reading
  783.      ***************
  784.  
  785.      It is inappropriate here to go into a detailed discussion of actual
  786.      coding and function of all the many standard FORTH words.  Many good
  787.      FORTH textbooks are now available which cover this subject admirably,
  788.      and two books by Leo Brodie, "Starting FORTH" and "Thinking FORTH",
  789.      stand out as being particularly well-informed and instructive.
  790.  
  791. *************************************************************************
  792. End
  793. *************************************************************************
  794.